using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Xml;
using AliCMS.SystemModule;

namespace AliCMS.Lib.SystemModule.Privilege
{
    public class List : AdminPageBase
    {
        public List(string pathInTemplate)
            : base(pathInTemplate)
        {
        }

        protected override void OnLoad(EventArgs e)
        {
            DataRow instance = Manager.Instances.Select(Alias.Instance.ID + "='" + CurrentInstanceID + "'")[0];

            #region ObjectTable
            using (AliCMS.SystemModule.Module.IObject bll = (AliCMS.SystemModule.Module.IObject)Manager.CreateObject("Module.Object", 0, ContentLang, false))
            {
                Dictionary<string, object> condition = new Dictionary<string, object>();
                condition[Alias.Module.Object.Namespace] = instance[Alias.Module.Namespace];
                _objectTable = bll.List(-1, -1, condition, null);
            }
            _objectTable.Columns.Add("FullName", typeof(string));

            foreach (DataRow row in _objectTable.Rows)
            {
                string name = row[Alias.Module.Object.Name].ToString();
                DataRow parent = row;
                while (!Guid.Equals(parent[Alias.Module.Object.ParentID], Guid.Empty))
                {
                    parent = _objectTable.Select(Alias.Module.Object.ID + "='" + parent[Alias.Module.Object.ParentID] + "'")[0];
                    name = parent[Alias.Module.Object.Name] + "." + name;
                }
                row["FullName"] = name;
            }
            #endregion

            #region PrivilegeTable
            using (IDbObject bll = (IDbObject)Manager.CreateObject("Privilege", InstanceID, ContentLang))
            {
                StringBuilder condition = new StringBuilder();
                condition.Append(Alias.Privilege.InstanceID + "=" + CurrentInstanceID);
                condition.Append(" AND " + Alias.Privilege.Lang + "='" + CurrentLang + "'");
                if (CurrentRoleID == Guid.Empty)
                    condition.Append(" AND " + Alias.Privilege.RoleID + " IS NULL");
                else
                    condition.Append(" AND " + Alias.Privilege.RoleID + "=" + CurrentRoleID);

                _privilegeTable = bll.List(-1, -1, condition.ToString(), null);
            }
            #endregion
        }

        protected bool HasPrivilege(DataRow record)
        {
            DataRow[] rows = _privilegeTable.Select(Alias.Privilege.ObjectID + "='" + record[Alias.Module.Object.Operation.ObjectID] + "' AND " + Alias.Privilege.OperationID + "='" + record[Alias.Module.Object.Operation.ID] + "'");
            if (rows.Length == 0)
                return false;
            else
                return true;
        }
        protected string GetPrivilegeID(DataRow record)
        {
            DataRow[] rows = _privilegeTable.Select(Alias.Privilege.ObjectID + "='" + record[Alias.Module.Object.Operation.ObjectID] + "' AND " + Alias.Privilege.OperationID + "='" + record[Alias.Module.Object.Operation.ID] + "'");
            if (rows.Length == 0)
                return String.Empty;
            else
                return rows[0][Alias.Privilege.InnerID].ToString();
        }

        #region Properties
        protected DataTable ObjectTable
        {
            get { return _objectTable; }
        }
        protected Guid CurrentRoleID
        {
            get
            {
                if (String.IsNullOrEmpty(Request.QueryString["RoleID"]))
                    return Guid.Empty;
                else
                    return new Guid(Request.QueryString["RoleID"]);
            }
        }
        protected Guid CurrentInstanceID
        {
            get
            {
                if (String.IsNullOrEmpty(Request.QueryString["InstanceID"]))
                    return (Guid)SystemChannel[Alias.Instance.ID];
                else
                    return new Guid(Request.QueryString["InstanceID"]);
            }
        }
        protected string CurrentLang
        {
            get
            {
                if (String.IsNullOrEmpty(Request.QueryString["CurrentLang"]))
                    return ContentLang;
                else
                    return Request.QueryString["CurrentLang"];
            }
        }
        #endregion

        #region Fields
        private DataTable _objectTable;
        private DataTable _privilegeTable;
        #endregion
    }
}
